home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
clean
/
sun3.lha
/
Sun3
/
seqdemos
/
war_seq.icl
< prev
Wrap
Text File
|
1992-08-07
|
2KB
|
74 lines
MODULE war_seq;
<<
Sequential version of Warshall's shortest path algorithm
Calculates the lenghts of the shortest paths between all nodes of
a directed graph represented by its adjacency matrix using Warshall's
algorithm. The result of the program will be a matrix containing the
length of the shortest path between node i and node j on index (i,j).
>>
FROM deltaI IMPORT --, ++, +, >;
TYPE
:: Row -> [INT]; == A row is represented as a list of integers.
:: Mat -> [Row]; == A matrix is represented as a list of rows.
MACRO
Size -> 6; == The size of the initial matrix.
RULE
== The initial matrix.
:: InitMat -> Mat; == Shortest path matrix:
InitMat -> [[ 0,100,100, 13,100,100 ], == [ 0, 16,100, 13, 20, 20 ]
[ 100, 0,100,100, 4, 9 ], == [ 19, 0,100, 5, 4, 9 ]
[ 11,100, 0,100,100,100 ], == [ 11, 27, 0, 24, 31, 31 ]
[ 100, 3,100, 0,100, 7 ], == [ 18, 3,100, 0, 7, 7 ]
[ 15, 5,100, 1, 0,100 ], == [ 15, 4,100, 1, 0, 8 ]
[ 11,100,100, 14,100, 0 ]]; == [ 11, 17,100, 14, 21, 0 ]
== Miscellaneous functions.
:: Min INT INT -> INT;
Min i j -> j, IF > i j
-> i;
:: Select [x] INT -> x;
Select [f|r] 1 -> f;
Select [f|r] k -> Select r (-- k);
== Warshall's shortest path algorithm.
:: Warshall Mat -> Mat;
Warshall mat -> Iterate 1 mat;
:: Iterate INT Mat -> Mat;
Iterate i mat -> mat, IF > i Size
-> Iterate (++ i) (WarRows i mat (Select mat i));
:: WarRows INT Mat Row -> Mat;
WarRows i [] rowi -> [];
WarRows i [rowj|rs] rowi
-> [ UpdateRow (Select rowj i) rowj rowi | WarRows i rs rowi ];
:: UpdateRow INT Row Row -> Row;
UpdateRow ji [] [] -> [];
UpdateRow ji [jk|rjs] [ik|ris]
-> [ Min jk (+ ji ik) | UpdateRow ji rjs ris ];
== The Start rule: apply Warshall's algorithm on the initial matrix.
:: Start -> Mat;
Start -> Warshall InitMat;